﻿package tbo.actors 
{
	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import tbo.actors.attacks.Attack;
	import tbo.events.TBOActorEvent;
	import tbo.model.Application;
	
	/**
	 * Generic template actor
	 * @author ...
	 */
	public class Actor extends Sprite
	{
	
		public static const END_MOVE:String = "endMove";
		
		
		protected var _hp:Number;
		protected var _chp:Number;
		protected var _power:Number;
		protected var _range:Number;
		public var gameName:String;
		public var _wx:Number;
		public var _wy:Number;
		public var _active:Boolean = true;
		
		public var image:Loader;
		
		public var _attacks:Array;
		
		
		public function Actor() 
		{
			image = new Loader();
			image.load(new URLRequest("data/img/player.png"));
			image.mouseEnabled = false;
			//image.graphics.beginFill(0xff0000);
			//image.graphics.drawRect(0, 0, 5, 10);
			//image.graphics.endFill();
			this.addChild(image);
		
			this.gameName = "Steve";
			
			_attacks = new Array();
			_attacks.push(new Attack());
			
			_wx = 0;
			_wy = 0;
			
			_range = 4;
			
			this.addEventListener(MouseEvent.MOUSE_DOWN, beginMove);
			this.addEventListener(MouseEvent.MOUSE_UP, endMove);
		}
	
		public function beginMove(e:MouseEvent):void {
			this.startDrag();
		}
		
		public function targetStart():void {
			dispatchEvent(new TBOActorEvent(TBOActorEvent.TARGET_START, this));
		}
		
		public function withinWalkingRange(wox:Number, woy:Number):Boolean {
			var diffX:Number = this._wx - wox;
			var diffY:Number = this._wy - woy;
			var total:Number = Math.abs(diffX) + Math.abs(diffY);
			trace(total);
			if (total > this._range) 
				return false;
			else 
				return true;
		}
		
		public function moveStart():void {
			dispatchEvent(new TBOActorEvent(TBOActorEvent.MOVE_START, this));
		}
		
		public function endMove(e:MouseEvent):void {
			this.stopDrag();
			dispatchEvent(new Event(END_MOVE));
		}
		
		public function applyDamage(d:Number):void {
			this._hp -= d;
			trace(this._hp);
			if (this._hp <= 0) {
				this._active = false;
				this.visible = false;
			}
		}
		
		public function get hp():Number {
			return _hp;
		}
		
		public function set hp(h:Number):void {
			_hp = h;
		}
		
		public function set currentHP(h:Number):void {
			_chp =  h;
		}
		
		public function get currentHP():Number {
			return _chp;
		}
		
		public function set power(p:Number):void {
			_power = p;
		}
		
		public function get power():Number {
			return _power;
		}
		
		public function get range():Number {
			return _range;
		}
		
	}
	
}